package operate

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Services/base"
	"hft/Http/Services/operate/operate_model"
	"hft/Http/Services/operate/operate_utils"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"hft/tools"
	"strconv"
)

// 运营人员
type Operate struct{}

// 添加运营人员
func (*Operate) Add(account *operate_model.OperateAccount, claims tools.BaseClaims) (err error) {
	fmt.Println(tools.JsonParse(account))
	err = tools.AliyunSmsTool.VerifyValid(global.OC_CONFIG.AliyunSms.AddAccountSend, account.Phone, account.Code)
	if err != nil {
		return err
	}
	fmt.Println("pass")
	parentAccount, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		fmt.Println("错误", err, parentAccount)
		return errors.New(e.AccountNotExist)
	}
	result, err := global.OC_REDIS.Get(fmt.Sprintf("SITE_LOGIN-%v", parentAccount.Username)).Result()
	fmt.Println("结构", result, fmt.Sprintf("SITE-LOGIN-%v", parentAccount.Username))
	if err != nil {
		return errors.New(e.PleaseLogin)
	}
	var belong, _ = strconv.Atoi(result)
	if belong == 1 || parentAccount.RegionLink == ",1," {
		return errors.New(e.PleaseLogin)
	}
	account.Belong = parentAccount.Belong
	account.RegionLink = parentAccount.RegionLink
	account.RoleName = "站点运营专员"
	account.RoleId = 34
	account.Status = true
	account.Link = ",1,19,32,34,"
	account.PID = parentAccount.ID
	account.AccountLink = fmt.Sprintf("%v%v,", parentAccount.AccountLink, parentAccount.ID)
	res := global.OC_DB.Where("phone = ? AND username = ?", account.Phone, account.Username).FirstOrCreate(&account.Account)
	if res.RowsAffected == 0 {
		return errors.New(e.AccountNotExist)
	}
	if res.Error != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

func (*Operate) Edit(account *operate_model.OperateAccount, claims tools.BaseClaims) (err error) {

	var nowAccount *admin.Account

	err = global.OC_DB.First(&nowAccount, account.ID).Error
	if err != nil {
		return errors.New(e.AccountNotExist)
	}

	if nowAccount.Phone != account.Phone {
		err = tools.AliyunSmsTool.VerifyValid(global.OC_CONFIG.AliyunSms.AddAccountSend, account.Phone, account.Code)
		if err != nil {
			return err
		}
	}

	parentAccount, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		return errors.New(e.AccountNotExist)
	}
	result, err := global.OC_REDIS.Get(fmt.Sprintf("SITE_LOGIN-%v", account.Username)).Result()
	if err != nil {
		return errors.New(e.PleaseLogin)
	}
	var belong, _ = strconv.Atoi(result)
	if belong == 1 || parentAccount.RegionLink == ",1," {
		return errors.New(e.PleaseLogin)
	}

	res := global.OC_DB.Model(&admin.Account{}).Where("id = ?", account.ID).Updates(map[string]interface{}{
		"username": account.Username,
		"phone":    account.Phone,
		"avatar":   account.Avatar,
		"sort":     account.Sort,
	})

	if res.RowsAffected == 0 {
		return errors.New(e.AccountNotExist)
	}
	if res.Error != nil {
		return errors.New(e.ErrorInfo)
	}

	return nil
}

func (*Operate) Delete(ids []int, claims tools.BaseClaims) (err error) {

	account, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		return errors.New(e.AccountNotExist)
	}
	var list []*admin.Account
	global.OC_DB.Find(&list, ids)
	if !operate_utils.IsBelongThisAccount(int(account.ID), list) {
		return errors.New(e.NoRightToOpera)
	}
	global.OC_DB.Unscoped().Delete(&list)

	return nil
}

func (*Operate) ChangeStatus(account *admin.Account, claims tools.BaseClaims) (err error) {
	parentAccount, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		return errors.New(e.AccountNotExist)
	}
	if parentAccount.Belong == 1 || parentAccount.RegionLink == ",1," {
		return errors.New(e.PleaseLogin)
	}
	res := global.OC_DB.Model(&admin.Account{}).Where("id = ?", account.ID).Update("status", account.Status)
	if res.RowsAffected == 0 {
		return errors.New(e.AccountNotExist)
	}
	if res.Error != nil {
		return errors.New(e.ErrorInfo)
	}
	return nil
}

// 获取运营列表
func (*Operate) List(paginate *utils.Page, claims tools.BaseClaims) (page *utils.Page, err error) {

	var accountList []*admin.Account

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return nil, errors.New(e.AccountNotExist)
	}

	result, err := global.OC_REDIS.Get(fmt.Sprintf("SITE_LOGIN-%v", account.Username)).Result()
	if err != nil {
		return nil, errors.New(e.PleaseLogin)
	}
	var belong, _ = strconv.Atoi(result)
	if account.Belong == 1 || account.RegionLink == ",1," {
		return nil, errors.New(e.PleaseLogin)
	}
	global.OC_DB.Scopes(dao.Paginate(dao.Page(*paginate), global.OC_DB)).Where("account_link LIKE ? AND role_id = ? AND belong = ?", fmt.Sprintf("%v%v,%%", account.AccountLink, account.ID), 34, belong).Find(&accountList).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)

	paginate.Data = accountList

	return paginate, nil
}

var OperateService = new(Operate)
